<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>同步折叠效果的侧边栏</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            min-height: 100vh;
            background-color: #f5f7fa;
            transition: margin-left 0.3s ease;
        }

        /* 侧边栏容器样式 */
        .sidebar {
            width: 250px;
            height: 100vh;
            background: linear-gradient(135deg, #3a416f 0%, #141727 100%);
            color: #fff;
            position: sticky;
            top: 0;
            left: 0;
            overflow-x: hidden;
            transition: all 0.3s ease;
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            z-index: 100;
            display: flex;
            flex-direction: column;
        }

        /* 折叠状态下的侧边栏 */
        .sidebar.collapsed {
            width: 70px;
        }

        /* 侧边栏头部样式 - 修改为左对齐 */
        .sidebar-header {
            padding: 20px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            height: 70px;
            overflow: hidden;
        }

        .logo {
            width: 30px;
            height: 30px;
            min-width: 30px;
            background-color: #fff;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #3a416f;
            font-weight: bold;
            margin-right: 10px;
            transition: margin-right 0.3s ease;
        }

        .sidebar.collapsed .logo {
            margin-right: 0;
        }

        .sidebar-header h3 {
            color: #fff;
            font-size: 1.2rem;
            white-space: nowrap;
            opacity: 1;
            transition: opacity 0.3s ease, width 0.3s ease;
            width: auto;
        }

        .sidebar.collapsed .sidebar-header h3 {
            opacity: 0;
            width: 0;
        }

        /* 菜单容器样式 */
        .sidebar-menu-container {
            flex: 1;
            overflow-y: auto;
            padding: 10px 0;
            overflow-x: hidden;
        }

        /* 菜单项通用样式 */
        .sidebar-menu {
            list-style: none;
            padding: 0;
        }

        .menu-item {
            position: relative;
        }

        .menu-item a, .menu-title {
            display: flex;
            align-items: center;
            padding: 12px 20px; /* 与头部对齐 */
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: all 0.3s ease;
            white-space: nowrap;
        }

        .menu-item a:hover, .menu-title:hover {
            background: rgba(255, 255, 255, 0.1);
            color: #fff;
        }

        .menu-item.active > a, .menu-item.active > .menu-title {
            background: rgba(255, 255, 255, 0.2);
            color: #fff;
        }

        /* 菜单图标样式 */
        .menu-icon {
            margin-right: 10px;
            font-size: 1.2rem;
            min-width: 25px;
            text-align: center;
            transition: margin-right 0.3s ease;
        }

        .sidebar.collapsed .menu-icon {
            margin-right: 0;
        }

        .menu-text {
            transition: opacity 0.3s ease, width 0.3s ease;
        }

        .sidebar.collapsed .menu-text {
            opacity: 0;
            width: 0;
            height: 0;
            overflow: hidden;
        }

        /* 子菜单样式 */
        .submenu {
            list-style: none;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease;
            background: rgba(0, 0, 0, 0.1);
        }

        .submenu.show {
            max-height: 500px;
        }

        .submenu .menu-item a {
            padding-left: 45px; /* 与主菜单对齐 */
        }

        /* 折叠状态下子菜单的样式 */
        .sidebar.collapsed .submenu {
            position: absolute;
            left: 70px;
            top: 0;
            width: 200px;
            background: #3a416f;
            border-radius: 0 5px 5px 0;
            box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
            display: none;
        }

        .sidebar.collapsed .menu-item:hover .submenu {
            display: block;
            max-height: 500px;
        }

        /* 子菜单指示箭头 */
        .menu-arrow {
            margin-left: auto;
            transition: transform 0.3s ease, opacity 0.3s ease;
        }

        .sidebar.collapsed .menu-arrow {
            opacity: 0;
            width: 0;
        }

        .menu-item.open .menu-arrow {
            transform: rotate(90deg);
        }

        /* 侧边栏底部样式 */
        .sidebar-footer {
            padding: 15px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed .sidebar-footer {
            justify-content: center;
        }

        .user-info {
            display: flex;
            align-items: center;
            white-space: nowrap;
            overflow: hidden;
        }

        .sidebar.collapsed .user-info {
            display: none;
        }

        .user-avatar {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: #3498db;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        .toggle-btn {
            background: none;
            border: none;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .toggle-btn:hover {
            color: #3498db;
            transform: scale(1.1);
        }

        /* Tooltip样式 */
        .menu-tooltip {
            position: absolute;
            left: 100%; /* 从父元素右侧开始 */
            top: 50%;
            transform: translateY(-50%) translateX(10px);
            background: #3a416f;
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 0.8rem;
            white-space: nowrap;
            z-index: 1000;
            display: none;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
            opacity: 0;
            transition: 
                opacity 0.2s ease,
                transform 0.2s ease;
            pointer-events: none; /* 防止Tooltip干扰鼠标事件 */
        }

        /* 修正选择器层级 */
        .sidebar.collapsed .menu-item:hover .menu-tooltip {
            display: block;
            opacity: 1;
            transform: translateY(-50%) translateX(0);
        }

        /* 主内容区域样式 */
        .main-content {
            flex: 1;
            padding: 20px;
            transition: margin-left 0.3s ease;
        }

        .main-content h1 {
            color: #333;
            margin-bottom: 20px;
        }

        /* 响应式设计 - 小屏幕下自动折叠 */
        @media (max-width: 768px) {
            .sidebar {
                width: 70px;
            }
            .sidebar-header h3 {
                opacity: 0;
                width: 0;
            }
            .menu-text {
                opacity: 0;
                width: 0;
                height: 0;
                overflow: hidden;
            }
            .main-content {
                margin-left: 70px;
            }
            .toggle-btn {
                transform: rotate(180deg);
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏结构 -->
    <aside class="sidebar">
        <div class="sidebar-header">
            <div class="logo">L</div>
            <h3>Logo名称</h3>
        </div>
        
        <div class="sidebar-menu-container">
            <ul class="sidebar-menu">
                <!-- 仪表盘菜单项 -->
                <li class="menu-item">
                    <a href="#">
                        <span class="menu-icon">📊</span>
                        <span class="menu-text">仪表盘</span>
                        <span class="menu-tooltip">仪表盘</span>
                    </a>
                </li>
                
                <!-- 带子菜单的用户管理 -->
                <li class="menu-item">
                    <div class="menu-title">
                        <span class="menu-icon">👥</span>
                        <span class="menu-text">用户管理</span>
                        <span class="menu-arrow">›</span>
                        <span class="menu-tooltip">用户管理</span>
                    </div>
                    <ul class="submenu">
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">用户列表</span>
                                <span class="menu-tooltip">用户列表</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">添加用户</span>
                                <span class="menu-tooltip">添加用户</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">权限设置</span>
                                <span class="menu-tooltip">权限设置</span>
                            </a>
                        </li>
                    </ul>
                </li>
                
                <!-- 产品管理 -->
                <li class="menu-item">
                    <a href="#">
                        <span class="menu-icon">📦</span>
                        <span class="menu-text">产品管理</span>
                        <span class="menu-tooltip">产品管理</span>
                    </a>
                </li>
                
                <!-- 带子菜单的订单管理 -->
                <li class="menu-item">
                    <div class="menu-title">
                        <span class="menu-icon">📝</span>
                        <span class="menu-text">订单管理</span>
                        <span class="menu-arrow">›</span>
                        <span class="menu-tooltip">订单管理</span>
                    </div>
                    <ul class="submenu">
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">所有订单</span>
                                <span class="menu-tooltip">所有订单</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">待处理</span>
                                <span class="menu-tooltip">待处理</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">已完成</span>
                                <span class="menu-tooltip">已完成</span>
                            </a>
                        </li>
                    </ul>
                </li>
                
                <!-- 设置 -->
                <li class="menu-item">
                    <a href="#">
                        <span class="menu-icon">⚙️</span>
                        <span class="menu-text">系统设置</span>
                        <span class="menu-tooltip">系统设置</span>
                    </a>
                </li>
                
                <!-- 带子菜单的统计报表 -->
                <li class="menu-item">
                    <div class="menu-title">
                        <span class="menu-icon">📈</span>
                        <span class="menu-text">统计报表</span>
                        <span class="menu-arrow">›</span>
                        <span class="menu-tooltip">统计报表</span>
                    </div>
                    <ul class="submenu">
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">销售统计</span>
                                <span class="menu-tooltip">销售统计</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">用户增长</span>
                                <span class="menu-tooltip">用户增长</span>
                            </a>
                        </li>
                        <li class="menu-item">
                            <a href="#">
                                <span class="menu-text">流量分析</span>
                                <span class="menu-tooltip">流量分析</span>
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>

        <!-- 侧边栏底部折叠按钮 -->
        <div class="sidebar-footer">
            <div class="user-info">
                <div class="user-avatar">A</div>
                <span>管理员</span>
            </div>
            <button class="toggle-btn" id="toggle-btn">◀</button>
        </div>
    </aside>

    <!-- 主内容区域 -->
    <main class="main-content" id="main-content">
        <h1>欢迎使用管理后台</h1>
        <p>点击底部按钮折叠/展开侧边栏，点击带箭头的菜单项展开子菜单。</p>
        <p>鼠标悬停在折叠后的菜单图标上会显示Tooltip提示。</p>
        <p>注意：侧边栏头部和菜单项的折叠效果现在完全同步。</p>
    </main>

    <script>
        // 等待DOM加载完成
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const toggleBtn = document.getElementById('toggle-btn');
            const sidebar = document.querySelector('.sidebar');
            const mainContent = document.getElementById('main-content');
            const menuTitles = document.querySelectorAll('.menu-title');
            
            // 切换侧边栏折叠状态
            toggleBtn.addEventListener('click', function() {
                sidebar.classList.toggle('collapsed');
                
                // 更新折叠按钮图标
                if (sidebar.classList.contains('collapsed')) {
                    toggleBtn.textContent = '›';
                } else {
                    toggleBtn.textContent = '‹';
                }
                
                // 如果是折叠状态，关闭所有子菜单
                if (sidebar.classList.contains('collapsed')) {
                    document.querySelectorAll('.submenu').forEach(submenu => {
                        submenu.classList.remove('show');
                    });
                    document.querySelectorAll('.menu-item').forEach(item => {
                        item.classList.remove('open');
                    });
                }
            });
            
            // 处理带子菜单的菜单项点击事件
            menuTitles.forEach(title => {
                title.addEventListener('click', function() {
                    // 如果侧边栏是折叠状态，不处理点击事件（因为会显示悬浮子菜单）
                    if (sidebar.classList.contains('collapsed')) return;
                    
                    const parentItem = this.parentElement;
                    const submenu = this.nextElementSibling;
                    
                    // 切换当前菜单项的open类
                    parentItem.classList.toggle('open');
                    
                    // 切换子菜单的显示/隐藏
                    if (submenu) {
                        submenu.classList.toggle('show');
                    }
                    
                    // 关闭其他打开的子菜单
                    document.querySelectorAll('.menu-item').forEach(item => {
                        if (item !== parentItem) {
                            item.classList.remove('open');
                            const otherSubmenu = item.querySelector('.submenu');
                            if (otherSubmenu) {
                                otherSubmenu.classList.remove('show');
                            }
                        }
                    });
                });
            });
            
            // 为所有菜单项添加点击激活状态
            document.querySelectorAll('.menu-item a').forEach(item => {
                item.addEventListener('click', function() {
                    document.querySelectorAll('.menu-item').forEach(i => {
                        i.classList.remove('active');
                    });
                    this.parentElement.classList.add('active');
                });
            });
            
            // 响应式设计：小屏幕下自动折叠侧边栏
            function handleResize() {
                if (window.innerWidth <= 768) {
                    sidebar.classList.add('collapsed');
                    toggleBtn.textContent = '›';
                } else {
                    sidebar.classList.remove('collapsed');
                    toggleBtn.textContent = '‹';
                }
            }
            
            // 初始检查和添加resize事件监听
            handleResize();
            window.addEventListener('resize', handleResize);
        });
    </script>
</body>
</html>
